"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
    return { next: verb(0), "throw": verb(1), "return": verb(2) };
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [0, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var common = require("../../../common");
module.exports = function (app) {
    return new Handler(app);
};
var Handler = function (app) {
    this.app = app;
};
var handler = Handler.prototype;
handler.updateSkin = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["skin"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.updateSkin(session, session.uid, msg.skin, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.buySkin = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["skin"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.buySkin(session, session.uid, msg.skin, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.updateTheme = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["theme"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.updateTheme(session, session.uid, msg.theme, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.buyTheme = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["theme"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.buyTheme(session, session.uid, msg.theme, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
// handler.bingPromoteCode = async function(msg, session, next) {
// 	let msgErr = common.getMsgError(msg, ["code"]);
// 	if (msgErr) { common.sendError(next, 501, msgErr); return; }
// 	this.app.rpc.user.userRemote.matching(session, session.uid, msg.code, (state) => {
// 		common.sendResult(next, { state: state });
// 	});
// };
handler.addFriend = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["userId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.addFriend(session, session.uid, msg.userId, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.verifyFriend = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["friendId", "isAgree"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.verifyFriend(session, session.uid, msg.friendId, msg.isAgree, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.removeFriend = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["friendId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.removeFriend(session, session.uid, msg.friendId, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.signIn = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            this.app.rpc.user.userRemote.signIn(session, session.uid, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.sendMessage = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["userId", "message"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.sendMessage(session, session.uid, msg.userId, msg.message, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.sendGlobalMessage = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["message"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.sendGlobalMessage(session, session.uid, msg.message, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.rename = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["name"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.rename(session, session.uid, msg.name, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.denaturation = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["isMale"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.denaturation(session, session.uid, msg.isMale, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.updateIconPath = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["iconPath"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.updateIconPath(session, session.uid, msg.iconPath, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.updateCoverUrl = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["coverUrl"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.updateCoverUrl(session, session.uid, msg.coverUrl, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.initCustomData = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["name", "isMale", "iconPath"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.initCustomData(session, session.uid, msg, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.readFriendMessage = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["friendId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.readFriendMessage(session, session.uid, msg.friendId, function (state, readAt) {
                common.sendResult(next, { state: state, readAt: readAt });
            });
            return [2 /*return*/];
        });
    });
};
handler.getDailyTaskClearList = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            this.app.rpc.user.userRemote.getDailyTaskClearList(session, session.uid, msg.taskId, function (result) {
                common.sendResult(next, result);
            });
            return [2 /*return*/];
        });
    });
};
handler.clearDailyTask = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["taskId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.clearDailyTask(session, session.uid, msg.taskId, function (state) {
                common.sendResult(next, { state: state });
            });
            return [2 /*return*/];
        });
    });
};
handler.getPlayingRoom = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            this.app.rpc.user.userRemote.getPlayingRoom(session, session.uid, function (roomNumber, isAlive) {
                common.sendResult(next, { roomNumber: roomNumber, isAlive: isAlive });
            });
            return [2 /*return*/];
        });
    });
};
handler.payment = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["productId", "platform", "orderId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.payment(session, session.uid, msg.productId, msg.platform, msg.orderId, next);
            return [2 /*return*/];
        });
    });
};
handler.getGiftRecords = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["userToId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.getGiftRecords(session, session.uid, msg.userToId, function (records) {
                common.sendResult(next, { records: records });
            });
            return [2 /*return*/];
        });
    });
};
handler.follow = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["userId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.follow(session, session.uid, msg.userId, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see :群组
 */
handler.createGroup = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["iconPath", "name", "manifesto", "createArea"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.createGroup(session, session.uid, msg.iconPath, msg.name, msg.manifesto, msg.createArea, next);
            return [2 /*return*/];
        });
    });
};
/**
 * @see  添加一条狼人杀比赛报名信息
 *
 * name     clubName     joinNumber    IdNumber  phone
 */
handler.insertOneMatchInfo = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr, res;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["name", "clubName", "IdNumber", "phone"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            res = checkRegx(msg.name, msg.clubName, msg.phone, msg.IdNumber);
            if (res.status != "0") {
                common.sendResult(next, { res: res });
                return [2 /*return*/];
            }
            // 这里需要对前端传入的数据  做校验     手机号 、身份证。
            this.app.rpc.user.userRemote.insertOneMatchInfo(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  根据当前user的uid查询----->当前用户的注册信息
 *
 * @main
 */
handler.findOneMatchInfoForUid = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            if (!session.uid) {
                common.sendError(next, 501, "msgErr");
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.findOneMatchInfoForUid(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  更新支付信息
 *
 * name     clubName     joinNumber    IdNumber  phone
 */
handler.updatePaymentInfoForMatch = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["orderId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.updatePaymentInfoForMatch(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
//=========================================Common  Method ===========================================
/**
 * @description
 * @author   xianfei             @email  zhouxf0425@gmail.com
 * @param {string} phone
 * @param {string} IdNumber
 */
function checkRegx(name, clubName, phone, IdNumber) {
    if (name == "" || name == null || name == undefined)
        return {
            status: "1",
            msg: "用户名称不能空！"
        };
    if (!checkNameillegalChar(name)) {
        return {
            status: "8",
            msg: "用户名称非法！"
        };
    }
    if (name.length > 20) {
        return {
            status: "9",
            msg: "输入的用户名称长度不能大于20！"
        };
    }
    if (clubName == "" || clubName == null || clubName == undefined)
        return {
            status: "4",
            msg: "俱乐部名不能空！"
        };
    if (phone == "" || phone == null || phone == undefined)
        return {
            status: "3",
            msg: "手机号码不能空！"
        };
    if (IdNumber == "" || IdNumber == null || IdNumber == undefined)
        return {
            status: "2",
            msg: "身份证不能空！"
        };
    if (!IdentityCodeValid(IdNumber))
        return {
            status: "5",
            msg: "输入的身份证号不合法！"
        };
    if (!isPhoneLegal(phone))
        return {
            status: "6",
            msg: "输入的手机不合法！"
        };
    return {
        status: "0",
        msg: "success"
    };
}
//身份证号合法性验证  142724199310044117
//支持15位和18位身份证号
//支持地址编码、出生日期、校验位验证
function IdentityCodeValid(code) {
    var cardReg = /^[1-9]([0-9]{14}|[0-9]{16}[0-9xX])$/;
    if (cardReg.test(code))
        return true;
    return false;
}
/**
 * @description 					验证手机是否合法
 * @author   xianfei             @email  zhouxf0425@gmail.com
 * @param {any} phone
 * @returns
 */
function isPhoneLegal(phone) {
    var phoneReg = /^1[34578]\d{9}$/;
    if (phoneReg.test(phone))
        return true;
    return false;
}
function checkNameillegalChar(name) {
    //字母   汉字  空格
    var nameReg = /^([\u4E00-\u9FA5]|[ a-zA-Z])*$/;
    if (nameReg.test(name))
        return true;
    return false;
}
/**
 * @see     用户兑换商品扣除积分
 *
 * name     clubName     joinNumber    IdNumber  phone
 */
handler.exchangeCreditsGoods = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["id", "count", "storeId"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            if (!session.uid) {
                common.sendError(next, 501, "msgErr");
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.exchangeCreditsGoods(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see    用户新增活着更新地址信息
 *
 *   必要传入的入参   name  ,  phone ，address
 */
handler.insertOrUpdateAddrInfo = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        var msgErr;
        return __generator(this, function (_a) {
            msgErr = common.getMsgError(msg, ["name", "phone", "address"]);
            if (msgErr) {
                common.sendError(next, 501, msgErr);
                return [2 /*return*/];
            }
            if (!session.uid) {
                common.sendError(next, 501, "!session.uid");
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.insertOrUpdateAddrInfo(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see   查询用户所有的礼物类型
 */
handler.findAllExistGoodsInfo = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            if (!session.uid) {
                common.sendError(next, 501, "!session.uid!");
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.findAllExistGoodsInfo(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
handler.getAddressInfo = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            if (!session.uid) {
                common.sendError(next, 501, "!session.uid");
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.getAddressInfo(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
/**
 * @see  获取所有的好友信息
 */
handler.getFriendsInfo = function (msg, session, next) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            if (!session.uid) {
                common.sendError(next, 501, "!session.uid");
                return [2 /*return*/];
            }
            this.app.rpc.user.userRemote.getFriendsInfo(session, session.uid, msg, function (res) {
                common.sendResult(next, { res: res });
            });
            return [2 /*return*/];
        });
    });
};
